package com.cts.learninglibrary.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cts.learninglibrary.pojo.Comment;
import com.cts.learninglibrary.service.CommentService;
import com.cts.learninglibrary.util.R;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/comment")
public class CommentController {

    @Autowired
    private CommentService commentService;

    /**
     * 查询分享id下的所有评论，并以二级树形结构组装
     */
    @GetMapping("/list/{share_id}")
    public R list(@PathVariable Integer share_id,
                  @RequestParam(defaultValue = "1") Integer pageNo,
                  @RequestParam(defaultValue = "10") Integer pageSize) {
        Page<Comment> page = new Page(pageNo,pageSize);
        List<Comment> entities = commentService.listWithTree(share_id);
        return R.ok(entities,"评论列表");
    }

    /**
     * 评论分页
     */
    @GetMapping("/listPage/{share_id}")
    public R listPage(@PathVariable Integer share_id,
                      @RequestParam(defaultValue = "1") Integer pageNo,
                      @RequestParam(defaultValue = "10") Integer pageSize) {
        List<Comment> entities = commentService.listWithTree(share_id);
        return R.ok(commentService.listWithTreeByPage(pageNo,pageSize,share_id),"评论列表");
    }

    /**
     * 发布评论
     * @param comment
     * @return
     */
    @PostMapping("/reply")
    public R reply(@RequestBody Comment comment) {
        commentService.reply(comment);
        return R.ok("评论成功");
    }

    /**
     * 删除评论
     * @param id
     * @return
     */
    @PostMapping("/deleteComment")
    public R deleteComment(@RequestParam Integer id) {
        //TODO 删除逻辑判断（当前评论是否为当前登陆用户的评论）后端获取当前用户信息

        commentService.deleteComment(id);
        return R.ok("删除成功");
    }

}
